<template>
  <div class="markTestPaper testPaper">
    <div class="w">
      <!-- 考试信息 -->
      <div class="title">
        <h3 class="testName">{{ testData.examName }}</h3>
        <ul>
          <li class="test-info">试卷Id: {{ testData.examId }}</li>
          <li class="test-info">出卷者: U{{testData.creatorId}}</li>
          <li class="test-info">答题时间: {{ testData.time }} 分钟</li>
          <li class="test-info">截至时间: {{ testData.examClasses.deadline }}</li>
          <li class="test-info">题目数量: 共 {{ testData.topicNum }} 道</li>
          <li class="test-info">总分: {{ testData.totalScore }} 分</li>
        </ul>

        <ul>
          <li class="user-info">所属班级: {{ testData.examClasses.classesName }}</li>
          <li class="user-info">答题人: {{ testData.userGrade.relaname }}</li>
          <li class="user-info">答题人Id: U{{ testData.userGrade.userId }}</li>
          <li class="user-info">自动评改得分: {{ testData.userGrade.gradeAuto }} 分</li>
          <li class="user-info">最终得分: {{ testData.userGrade.grade }} 分</li>
          <li class="fr">
            <el-button type="primary" size="mini" @click="submitTestpaper" >提交试卷</el-button>
          </li>
        </ul>
      </div>

      <div class="title fixed" v-if="isFixed">
        <ul>
          <li class="test-info"><strong class="testName">{{ testData.name }}</strong></li>
          <li class="test-info">总分: {{ testData.totalScore }} 分</li>
          <li class="test-info">答题人: {{ testData.userGrade.userName }}</li>
          <li class="test-info">最终得分: {{ testData.userGrade.grade }} 分</li>
          <li class="fr">
            <el-button type="primary" size="mini" @click="submitTestpaper" >提交试卷</el-button>
          </li>
        </ul>
      </div>

      <div class="test-content">
        <!-- 题目内容 -->
        <div class="topics">
          <div class="topic" v-for="(topics, index) in sortedTopics" :key="index">

            <div class="topicsType" v-if="topics.topic_content.length != 0 ">
              <h4>{{bigQuestionName_mixin(topics.topic_type,index)}}</h4><!-- 题目类型名称 -->
              <ul>
                <li class="topic-content" v-for="(t, index) in topics.topic_content" :key="index">
                  <div class="_location"></div>
                  <!-- 题目 -->
                  <div class="left">
                    <div class="question">
                      <span class="question_nunber">{{ topicNavIndex_mixin(topics.topic_type,index) }}、</span>
                      {{ t.question }}
                      <span class="score">({{ t.score }}分)</span>
                    </div>

                    <!-- 单选题 -->
                    <div class="radio" v-if="t.topicType==0">
                      <el-radio v-for="(item, index) in t.choice" :key="index"
                                :class="item == t.correctAnswer? 'correct':'error'"
                                v-model="t.userAnswer"
                                :label="item"
                                :disabled="isRead">
                        {{String.fromCharCode(65+index)}}、{{ item }}
                      </el-radio>
                    </div>

                    <!-- 多选题 -->
                    <div class="checkbox" v-if="t.topicType == 1">
                      <el-checkbox-group v-model="t.userAnswer">
                        <el-checkbox :label="item" v-for="(item, index) in t.choice" :key="index"
                                     :disabled="isRead"
                                     :class="isCheckboxCorrect(t,item)" >
                          {{String.fromCharCode(65+index)}}、{{ item }}
                        </el-checkbox>
                      </el-checkbox-group>
                    </div>

                    <!-- 判断题 -->
                    <div class="TrueOrFalse" v-if="t.topicType == 2">
                      <el-radio v-model="t.userAnswer" label="true" :class="'true' == t.correctAnswer? 'correct':'error'" :disabled="isRead">正确</el-radio>
                      <el-radio v-model="t.userAnswer" label="false" :class="'false' == t.correctAnswer? 'correct':'error'" :disabled="isRead">错误</el-radio>
                    </div>

                    <!-- 填空题 -->
                    <div class="fillInBlank" v-if="t.topicType == 3">
                      <div v-for="(q, index) in t.correctAnswer" :key="index">
                        <el-input type="textarea" autosize placeholder="(学生没有回答)" :disabled="isRead" v-model="t.userAnswer[index]">
                        </el-input>
                      </div>
                    </div>

                    <!-- 简答题 -->
                    <div class="text" v-if="t.topicType == 4">
                      <el-input type="textarea" v-model="t.userAnswer" :autosize="{ minRows: 3, maxRows: 10 }" placeholder="(学生没有回答)" :disabled="isRead">
                      </el-input>
                    </div>

                    <div v-if="t.topicType == 3||t.topicType == 4" class="correctAnswer">参考答案: {{t.correctAnswer}}</div>
                  </div>
                  <div class="right">
                    <div v-if="t.topicType == 0||t.topicType == 1||t.topicType == 2">
                      <div class="correctAnswer">参考答案: {{correctOptions(t)}}</div>
                      <div class="userScore">得分: {{t.userScore}} 分</div>
                    </div>
                    <div v-else>
                      <div class="userScore">
                        得分:
                        <el-input-number v-model="t.userScore" controls-position="right" :min="0" :max="t.score"></el-input-number>
                      </div>
                    </div>
                  </div>
                </li>
              </ul>
            </div>

          </div>
        </div>

        <!-- 题目导航 -->
        <div class="topic-nav " :class="isFixed?'isFixed':''" :style="topic_nav_style">
          <div class="topic-nav-describe">
            <span class="topic-nav-but correct"> </span> 正确
            <span class="space"></span>
            <span class="topic-nav-but error"> </span> 错误
          </div>

          <div v-for="(topics, Topics_index) in sortedTopics" :key="Topics_index">

            <div class="topic-nav-item" v-if="topics.topic_content.length != 0">
              <div class="nav-title">{{topicTypeName_mixin(topics.topic_type)}}</div>

              <span class="topic-nav-button" @click="topicNav(topics.topic_type,index)" v-for="(item , index) in topics.topic_content" :key="index" :class="isMarkTopic(item)">
                {{topicNavIndex_mixin(topics.topic_type,index)}}
              </span>
            </div>

          </div>
        </div>

      </div>
      <div class="back-top" @click="backTop_mixin()">
        <i class="el-icon-arrow-up"></i>
      </div>

    </div>
  </div>
</template>

<script>
import axios from "@/utils/axiosutils.js"
// import "@/assets/less/testPaper.less";
import testPaperMixin from "@/mixins/testPaper-mixin";
// import "@/assets/less/markTestPaper.less";

export default {
  name: "MarkTestPaper",
  mixins: [testPaperMixin],
  data() {
    return {
      classesId: this.$route.query.cId,
      examId: this.$route.query.eId,
      userId: this.$route.query.uId,


      //按题目类型分类好的题目数据
      //题目类型==>  0:单选题  1:多选题  2:判断题  3:填空题  4:简答题
      sortedTopics: [
        { topic_type: 0, topic_content: [] },
        { topic_type: 1, topic_content: [] },
        { topic_type: 2, topic_content: [] },
        { topic_type: 3, topic_content: [] },
        { topic_type: 4, topic_content: [] },
      ],
      //试卷数据
      testData: {
        examClasses:{},
        userGrade:{}
      },
      isRead: true, //是否为只读模式
      //侧导航栏是否悬浮
      isFixed: false,
      topic_nav_style: "top:0px",
    };
  },

  created() {
    this.getTestPaperData();
  },

  mounted() {
    // 监听滚动事件，然后用handleScroll这个方法进行相应的处理
    window.addEventListener("scroll", this.handleScroll);
  },

  methods: {
    //提交试卷
    submitTestpaper() {
      let userTopicList =[]
      let grade = 0;
      this.testData.topicTchDTOList.forEach(item =>{
        grade += item.userScore
        userTopicList.push({
          classesId: this.classesId,
          userId: this.userId,
          examId: this.examId,
          topicId: item.topicId,
          userScore: item.userScore,
        })
      })
      let request = {
        classesId: this.classesId,
        userId: this.userId,
        examId: this.examId,
        grade: grade,
        userTopicList: userTopicList,
      };
      let params=JSON.stringify(request)
      axios.post("/markExam/tchMarkExam",resp=>{
		  this.$message.success('批改完成');
	  },{json : params});
      
    },

    //获取试卷数据
    getTestPaperData() {
      let params = {
        classesId: this.classesId,
        examId: this.examId,
        userId: this.userId,
      }
      axios.get("markExam/getStuExamInfo",res=>{
        console.log(res)
        this.processTestPaperData(res.obj);
      },params)
    },

    //处理试卷的题目数据
    processTestPaperData(testData) {
      testData.topicTchDTOList.forEach((item, index) => {
        //按换行符分割字符串
        item.choice = item.choice.split(/[\n]/g);
        item.correctAnswer = item.correctAnswer.split(/[\n]/g);
        //添加空用户答案
        if (item.topicType == 1 || item.topicType == 3) {
          item.userAnswer = [];
        } else {
          item.userAnswer = "";
        }
        //添加教师是否批改判断
        item.status = testData.userTopicList[index].topicStatus;
        //添加评改分数
        item.userScore = testData.userTopicList[index].userScore;
      });


      //处理用户答案数据
      testData.userTopicList.forEach((item, index) => {
        //按换行符分割字符串
        if (item.userAnswer.indexOf("\n") != -1) {
          item.userAnswer = item.userAnswer.split(/[\n]/g);
        }
        if (testData.topicTchDTOList[index]!=null){
          testData.topicTchDTOList[index].userAnswer = item.userAnswer;
        }
        // testData.topicTchDTOList[index].userAnswer = item.userAnswer;
        // console.log(testData.topicTchDTOList[index].userAnswer);
      });
      //根据题目id写入用户答案


      this.testData = testData;
      console.log("this.testData ==> ", this.testData);

      //按题目类型分类并保存
      var topics = this.testData.topicTchDTOList;
      var topicsIndex = 1;
      for (let i = 0; i < topics.length; i++) {
        for (let item of this.sortedTopics) {
          if (topics[i].topicType == item.topic_type) {
            //添加
            topics[i].index = topicsIndex++;
            item.topic_content.push(topics[i]);
          }
        }
      }
      console.log("this.sortedTopics ==> ", this.sortedTopics);

    },

    //滚动事件
    handleScroll() {
      let scrollTop =
          window.pageYOffset ||
          document.documentElement.scrollTop ||
          document.body.scrollTop; // 滚动条偏移量
      if (scrollTop > 154) {
        this.topic_nav_style = "top:" + (scrollTop + 62) + "px";
        this.isFixed = true;
      } else {
        this.isFixed = false;
      }
    },

    //点击题号定位到题目位置
    topicNav(type, index) {
      var i = this.topicNavIndex_mixin(type, index);
      console.log(i);
      document
          .getElementsByClassName("_location")
          [i - 1].scrollIntoView({ behavior: "smooth", block: "start" });
    },

    //判断题目是否批改与正确
    isMarkTopic(topic) {
      //单选题/多选题/判断题/
      if (topic.topicType == 0 || topic.topicType == 2 || topic.topicType == 1) {
        if(topic.score == topic.userScore){
          return "correct";
        } else {
          return "error";
        }
      }

      //如果为自动评分
      if (this.testData.autoMack == 1) {
        if(topic.score == topic.userScore){
          return "correct";
        } else {
          return "error";
        }
      }
      // } else {
      //   if (topic.status == 1) return "correct";
      //   if (topic.status == 2) return "error";
      // }
    },

    isCheckboxCorrect(topic,val){
      // console.log(topic ,val);
      let is = false
      topic.correctAnswer.forEach(item =>{
        if(item == val){
          console.log(item);
          is = true
        }
      })
      if(is){
        return "correct";
      } else {
        return "error";
      }
    },

    //正确选项参考答案选项字母化
    correctOptions(topic) {
      var correctOptions = "";
      switch (topic.topicType) {
        case 0:
          for (let i = 0; i < topic.choice.length; i++) {
            if (topic.choice[i] == topic.correctAnswer[0]) {
              correctOptions = String.fromCharCode(65 + i);
              break;
            }
          }
          break;

        case 1:
          for (let i = 0; i < topic.choice.length; i++) {
            for (let j = 0; j < topic.correctAnswer.length; j++) {
              if (topic.choice[i] == topic.correctAnswer[j]) {
                correctOptions += String.fromCharCode(65 + i);
                continue;
              }
            }
          }
          break;

        case 2:
          if (topic.correctAnswer[0] == "true") {
            correctOptions = "正确";
          } else {
            correctOptions = "错误";
          }
          break;
      }

      return correctOptions;
    },
  },
};
</script>

<style lang="scss" scoped>
.markTestPaper{
  .w{
    width: 1400px;
  }
  .title.fixed{
    width: 1380px;
  }
  .topics{
    width: 1012px;
  }
  h4{
    margin: 24px 0 0 0;
  }
  .topic-content{
    display: flex;
    padding: 24px 8px;
    border-bottom: 1px solid #DCDFE6;

    .left{
      width: 800px;
    }
    .left .correctAnswer{
      padding: 12px  0;
      font-size: 16px;
    }

    .right{
      padding: 0 24px;
      font-size: 16px;
    }
    .right .correctAnswer{
      padding: 12px  0;
      font-size: 16px;
    }
  }
  .el-radio.is-disabled.is-checked .el-radio__input.is-disabled + span.el-radio__label,
  .el-checkbox.is-disabled.is-checked .el-checkbox__input.is-disabled + span.el-checkbox__label{
    color: #ff6b6b;
  }

  .topics .topic:last-child ul li:last-child{
    border: none;
  }




}#app {
   overflow: visible;
   background: #d3ebff;
 }

html {
  background: #d3ebff;
}

.w {
  position: relative;
  width: 1200px;
  margin: 0 auto;
  padding: 0 10px;
  box-sizing: border-box;
  padding-bottom: 40px;
}

.testPaper .title,
.topics,
.topic-nav {
  background: #fafafa;
  border-radius: 2px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
}

.testPaper {
  .test-content {}

  .topics {
    float: left;
    width: 812px;
    box-sizing: border-box;
    padding: 16px;
    margin-bottom: 30px;
  }
}


.testPaper .title {
  // height: 128px;
  padding: 10px 30px 10px 30px;
  margin-bottom: 10px;

  .testName .el-input__inner {
    margin: 0 0 20px 40px;
    border: none;
    background: rgba(255, 255, 255, 0);
    font-size: 20px;
    color: #333;
    font-weight: bold;
    text-decoration: underline;
  }

  ul {
    margin-bottom: 10px;
  }

  ul::after {
    content: '';
    display: block;
    clear: both;
  }

  .test-info {
    float: left;
    margin-right: 40px;

    i {
      color: #333;
      position: relative;
      top: 2px;
      left: 2px;

    }
  }

  .test-info:last-child {
    margin: 0;
  }

  .user-info {
    float: left;
    margin-top: 10px;
    margin-right: 40px;
  }

  .el-input-number {
    width: 72px;
  }

  .el-input-number.is-controls-right .el-input__inner {
    padding-left: 0px;
    padding-right: 28px;
  }
}

.testPaper .title.fixed {
  position: fixed;
  z-index: 999;
  top: 0;
  width: 1180px;
  box-sizing: border-box;
  padding: 14px 30px 0px 30px;
}



.topics .topic {
  .bigQuestionName {
    position: relative;
    display: flex;
    justify-content: space-between;
  }

  .bigQuestionName h4 {
    margin: 20px 0;
  }

  .bigQuestionName .allScore {
    margin: 20px 0;

  }

  .bigQuestionName .allScore button {
    height: 28px;
  }

  .bigQuestionName .allScore button.active {
    background: #666;
    color: #fff;
  }

  .bigQuestionName .allScore .el-input {
    position: absolute;
    top: -30px;
    right: 0;
    width: 120px;

  }

  .bigQuestionName .allScore .el-input::before {
    content: '';
    display: inline-block;
    position: absolute;
    bottom: -4px;
    left: 7px;
    transform: rotate(-45deg);
    width: 8px;
    height: 8px;
    border: 1px solid transparent;
    border-left: 1px solid #DCDFE6;
    border-bottom: 1px solid #DCDFE6;
  }

  .bigQuestionName .allScore .el-input::after {
    content: '';
    display: inline-block;
    position: absolute;
    bottom: -3px;
    left: 8px;
    transform: rotate(-45deg);
    border: 4px solid transparent;
    border-left: 4px solid #fff;
    border-bottom: 4px solid #fff;
  }


  .topic-content {
    position: relative;
    padding: 10px 8px;
    border-radius: 8px;
  }

  .topic-content.isEdit {
    // background: #f5f5f5;
  }

  .topic-content ._location {
    position: absolute;
    top: -50px;
    width: 10px;
    height: 10px;
  }

  .required-symbol{
    color: red;
  }

  .question {
    margin-bottom: 20px;
    font-size: 16px;
    line-height: 26px;
    color: #333;
  }

  .question .el-textarea {
    width: 730px;
  }


  .el-radio,
  .el-checkbox {
    width: 600px;
    margin-bottom: 2px;
  }

  .userAnswer .el-textarea {
    width: 500px;
  }

  .userAnswer .topicNavIndex {
    display: inline-block;
    width: 30px;
  }

  .score {
    float: right;
  }

  .addRadios {
    margin: 4px 0 0 30px;
  }

  .delRadios.el-button {
    position: relative;
    top: -2px;
    padding: 4px 4px;
    margin-left: 12px;
  }

  .correctAnswer{
    padding-left: 12px;
  }

  .topicScore,
  .difficulty,
  .required {
    display: inline-block;
    box-sizing: border-box;
    width: 33%;
    padding: 0 12px;
    margin-bottom: 10px;
    font-size: 14px;
  }

  .difficulty .el-input {
    width: 120px;
  }

  .analysis,
  .correct_answer {
    padding: 0 10px;
    font-size: 14px;
  }

  .el-radio,
  .el-checkbox {
    display: block;
    margin-left: 20px;
    color: #222;
  }

  .el-radio__label,
  .el-checkbox__label {
    font-size: 16px;
    line-height: 30px;
    word-wrap: break-word;
    white-space: normal;
  }

  .el-checkbox__input {
    float: left;
    margin-top: 8px;
  }

  .fillInBlank .el-textarea {
    width: 50%;
    margin-left: 30px;
    margin-top: 10px;
  }

  .text .el-textarea {
    width: 400px;
    margin: 4px 0 0 30px;
  }

  .text .addRadios {
    margin-bottom: 10px;
  }

  .newTopic {
    margin: 10px 0;
    padding: 10px 0;
    text-align: center;
    color: #64a9e3;
    border: 1px solid #64a9e3;
    cursor: pointer;
  }

}



.testPaper .topic-nav {
  float: right;
  box-sizing: border-box;
  width: 356px;
  min-height: 300px;
  padding: 14px;

  .tool {
    display: flex;
    justify-content: space-between;
    width: 200px;
    height: 30px;
    margin: 0 auto;


    .el-button {
      padding: 6px 10px;
    }
  }

  .topic-nav-describe {
    width: 140px;
    margin: 0 auto;
    font-size: 14px;

    .topic-nav-but {
      display: inline-block;
      position: relative;
      top: 4px;
      width: 16px;
      height: 16px;
      color: #64a9e3;
      border: 1px solid #64a9e3;
      border-radius: 2px;
    }

    .topic-nav-but.hasAnswer {
      background-color: #64a9e3;
      color: #fff;
    }

    .space {
      display: inline-block;

      width: 20px;
    }
  }

  .topic-nav-item {
    margin-bottom: 10px;
  }

  .nav-title {
    width: 50px;
    font-size: 14px;
    margin-left: 10px;
  }

  .topic-nav-button {
    display: inline-block;
    width: 30px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    margin: 10px 6px 0 8px;
    color: #64a9e3;
    border: 1px solid #64a9e3;
    border-radius: 2px;
    cursor: pointer;
    transition: 0.5s;
  }

  .topic-nav-button.hasAnswer {
    background-color: #64a9e3;
    color: #fff;
  }
}

.topic-nav.isFixed {
  position: absolute;
  top: 154px;
  right: 10px;
  // transform: 0.5s;
}

.testPaper .back-top {
  position: fixed;
  bottom: 50px;
  right: 100px;
  background: #fff;
  color: #333;
  font-size: 18px;
  border-radius: 2px;
  padding: 6px 8px;
  cursor: pointer;
}

.el-radio.is-disabled.is-checked .el-radio__input.is-disabled+span.el-radio__label,
.el-checkbox.is-disabled.is-checked .el-checkbox__input.is-disabled+span.el-checkbox__label {
  color: #409EFF;
}

.topic-nav-button.correct,
.topic-nav-but.correct{
  background:#02c39a !important;
  border:1px solid #02c39a !important;
  color: #fff !important;
}
.topic-nav-button.error,
.topic-nav-but.error{
  background: #ff6b6b;
  border:1px solid #ff6b6b !important;
  color: #fff !important;

}

.test-content.publishScore .el-radio.is-disabled.is-checked .el-radio__input.is-disabled + span.el-radio__label,
.test-content.publishScore .el-checkbox.is-disabled.is-checked .el-checkbox__input.is-disabled + span.el-checkbox__label{
  color: #ff6b6b;
}

.el-radio.is-disabled.is-checked.correct .el-radio__input.is-disabled + span.el-radio__label,
.el-checkbox.is-disabled.is-checked.correct .el-checkbox__input.is-disabled + span.el-checkbox__label{
  color: #02c39a !important;
}


.topic-content .correct .el-radio__label{
  color: #02c39a !important;
}


.testPaper .forbid_copy {
  -moz-user-select: none;
  /*火狐*/
  -webkit-user-select: none;
  /*webkit浏览器*/
  -ms-user-select: none;
  /*IE10*/
  -khtml-user-select: none;
  /*早期浏览器*/
  user-select: none;
}


</style>